﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class UserMessageBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IUserMessageDA userMessageDA = DataAccess.CreateUserMessageDA();

        public UserMessageBLL()
        {
            this.pubParasInfo = null;
            this.dataBaseID = new List<string>();
            this.dataBaseID.Add(ConstUtils.CONST_GLOBAL_TABLE);
        }

        public UserMessageBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public UserMessageBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = new List<string>();
                this.dataBaseID.Add(ConstUtils.CONST_GLOBAL_TABLE);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> UserMessageID)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            int result = 0;
            try
            {
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                result = userMessageDA.DeleteUserMessage(UserMessageID, this.dataBaseID[0]);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result.ToString();
        }

        public string DeleteSendMessage(List<string> UserMessageID)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            int result = 0;
            try
            {
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                result = userMessageDA.DeleteSendMessage(UserMessageID, this.dataBaseID[0]);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result.ToString();
        }

        public DataSet GetAllInfo()
        {
            return userMessageDA.GetAllUserMessage(this.dataBaseID[0]);
        }

        public DataSet GetAllUnreadUserMessage()
        {
            return userMessageDA.GetAllUnreadUserMessage(this.dataBaseID[0]);
        }

        public object GetInfoByID(string userMessageID)
        {
            return userMessageDA.GetUserMessageByID(userMessageID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            UserMessageQueryEntity userMessageQuery = obj as UserMessageQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(userMessageQuery.FromUserID))
            {
                sqlWhere.Add(" M.FROM_USER_ID " + userMessageQuery.FromUserID);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.ToUserID))
            {
                sqlWhere.Add(" M.TO_USER_ID " + userMessageQuery.ToUserID);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.MessageTitle))
            {
                sqlWhere.Add(" M.MESSAGE_TITLE " + userMessageQuery.MessageTitle);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.MessageContent))
            {
                sqlWhere.Add(" M.MESSAGE_CONTENT " + userMessageQuery.MessageContent);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.SystemCreated))
            {
                sqlWhere.Add(" M.SYSTEM_CREATED " + userMessageQuery.SystemCreated);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.IsRead))
            {
                sqlWhere.Add(" M.IS_READ " + userMessageQuery.IsRead);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.IsActive))
            {
                sqlWhere.Add(" M.IS_ACTIVE " + userMessageQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.CreatedBy))
            {
                sqlWhere.Add(" M.CREATED_BY " + userMessageQuery.CreatedBy);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.CreatedDate))
            {
                sqlWhere.Add(" M.CREATED_DATE " + userMessageQuery.CreatedDate);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.UpdatedBy))
            {
                sqlWhere.Add(" M.UPDATED_BY " + userMessageQuery.UpdatedBy);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.UpdatedDate))
            {
                sqlWhere.Add(" M.UPDATED_DATE " + userMessageQuery.UpdatedDate);
            }
            return userMessageDA.GetUserMessageByQueryList(sqlWhere, userMessageQuery, this.dataBaseID[0]);
        }

        public DataSet GetInMessageByQueryEntity(object obj)
        {
            UserMessageQueryEntity userMessageQuery = obj as UserMessageQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(userMessageQuery.FromUserID))
            {
                sqlWhere.Add(" M.FROM_USER_ID " + userMessageQuery.FromUserID);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.ToUserID))
            {
                sqlWhere.Add(" M.TO_USER_ID " + userMessageQuery.ToUserID);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.MessageTitle))
            {
                sqlWhere.Add(" M.MESSAGE_TITLE " + userMessageQuery.MessageTitle);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.MessageContent))
            {
                sqlWhere.Add(" M.MESSAGE_CONTENT " + userMessageQuery.MessageContent);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.SystemCreated))
            {
                sqlWhere.Add(" M.SYSTEM_CREATED " + userMessageQuery.SystemCreated);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.IsRead))
            {
                sqlWhere.Add(" M.IS_READ " + userMessageQuery.IsRead);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.IsActive))
            {
                sqlWhere.Add(" M.IS_ACTIVE " + userMessageQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.CreatedBy))
            {
                sqlWhere.Add(" M.CREATED_BY " + userMessageQuery.CreatedBy);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.CreatedDate))
            {
                sqlWhere.Add(" M.CREATED_DATE " + userMessageQuery.CreatedDate);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.UpdatedBy))
            {
                sqlWhere.Add(" M.UPDATED_BY " + userMessageQuery.UpdatedBy);
            }
            if (!string.IsNullOrEmpty(userMessageQuery.UpdatedDate))
            {
                sqlWhere.Add(" M.UPDATED_DATE " + userMessageQuery.UpdatedDate);
            }
            return userMessageDA.GetInMessageByQueryList(sqlWhere, userMessageQuery, this.dataBaseID[0]);
        }

        public object GetUserUnreadMessageByID(string userID)
        {
            return userMessageDA.GetUserUnreadMessageByID(userID, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            int result = 0;
            try
            {
                UserMessageInfo userMessageInfo = obj as UserMessageInfo;
                if (userMessageInfo != null)
                {
                    result = userMessageDA.InsertUserMessage(userMessageInfo, this.dataBaseID[0]);
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return result.ToString();
        }

        public string InsertList(object obj)
        {
            int result = 0;
            try
            {
                List<UserMessageInfo> userMessageInfoList = obj as List<UserMessageInfo>;
                if (userMessageInfoList != null)
                {
                    result = userMessageDA.InsertUserMessageList(userMessageInfoList, this.dataBaseID[0]);
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return result.ToString();
        }

        public string Update(object obj)
        {
            string messageStr = "";
            try
            {
                UserMessageInfo userMessageInfo = obj as UserMessageInfo;
                if (userMessageInfo == null)
                {
                    return messageStr;
                }
                if (userMessageDA.UpdateUserMessage(userMessageInfo, this.dataBaseID[0]) > 0)
                {
                    messageStr = userMessageInfo.MessageTitle;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return messageStr;
        }

        public string UpdateMarkUserMessage(string rowIDs, string isRead)
        {
            int result = 0;
            try
            {
                result = userMessageDA.UpdateMarkUserMessage(rowIDs, isRead, this.dataBaseID[0]);
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return result.ToString();
        }
    }
}

